home *** CD-ROM | disk | FTP | other *** search
/ Aminet 6 / Aminet 6 - June 1995.iso / Aminet / misc / sci / RARS_Amiga_2.lha / RARS / cntrlr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-31  |  10.1 KB  |  215 lines

  1. // CNTRLR.CPP - "driver" function for RARS - M. Timin, March, 1995
  2. // adapted to ver. 0.4 3/12/95 by M. Timin
  3.  
  4.              /* Commented Robot Driver */
  5. /*
  6. This robot driver calculates a cornering speed for each corner based on its
  7. radius.  He accelerates on each straightaway until a certain fraction
  8. of its length is reached; Then he slows down, attempting to arrive at
  9. the corner with the proper cornering speed.  In the corner he attempts
  10. to maintain the cornering speed while attempting to stay in the middle
  11. of the track.  His strategy for passing is to choose right or left at
  12. random, and then throw the car into a sharp slide toward that direction.
  13. */
  14.  
  15. #include <string.h>
  16. #include <stdlib.h>
  17. #include <math.h>
  18. #include "car.h"
  19.  
  20. extern char* glob_name;       // The name string, below, will be copied here
  21.  
  22. // This structure will be built in the data area provided by the caller:
  23. struct params {   
  24.    double CORN_SPD_CON;   // determines how fast to take corners
  25.    double STEER_GAIN;     // servo gain, for staying in "lane"
  26.    double STEER_DAMP;     // servo damping, to prevent "weaving"
  27.    double END_ACCEL; // we accelerate until this fraction of length
  28.    double END_CORNER;// used to decide when to start leaving the corer
  29.    double SLIP_LIM;  // maximum wheel slip, ft/sec, in wheel_slip()
  30.    double SLIP_CON;
  31.    double BIAS;
  32.    double NEAR_END;
  33.    double SHARP_TURN; // change in alpha when attempting to pass
  34.    int PASSING_TIME;  // time to stay in passing maneuver, counts
  35. };
  36.  
  37. inline double ABS(double arg) { return arg < 0.0 ? -arg : arg; }
  38.  
  39. // The following function calculates the speed for a corner.
  40. // The lateral force produced by cornering is proportional to the square
  41. // of the speed, and is inversely proportional to the radius of the path.
  42. // Therefore, the attainable cornering speed for differnt radii is
  43. // proportional to the square root of the radius.  This function implements
  44. // that rule.  The value to use for CORN_SPD_CON can be determined by
  45. // trial and error.  Example result: if the car is following a path with
  46. // radius of 100 ft, and if CORN_SPD_CON is 5.0, then corn_speed is 50 ft/sec.
  47. double corn_speed(double radius, double param)
  48. {
  49.    if(radius < 0.0)         // change sign of negative radius
  50.       radius = -radius;
  51.    else if(radius == 0.0)   // This is just insurance, this funtion doesn't
  52.       return(200.0);        // make sense when the radius is zero.
  53.    return param * sqrt(radius+15.0);
  54. }
  55.  
  56. // In order to set vc, if you know how fast you want to go (goal), and how
  57. // fast you are going now (present), This function will compute a reasonable
  58. // value for vc.  The value is never very far from the present speed, both
  59. // to attempt to stay within the power limit, and to maintain steering control.
  60. // You can adjust the resulting slip by changing "param".
  61. double wheel_speed(double goal, double present, double param)
  62. {
  63.    double ws;
  64.  
  65.    if(present > goal + 2 * param)  // if too fast,
  66.       ws = present - param;      // slow down.
  67.    else if(present < goal - 2 * param)  // if too slow,
  68.       ws = present + param;             // accelerate.
  69.    else                           // if quite close,
  70.       ws = (goal + present) / 2;      // approach desired speed gently.
  71.  
  72.    return ws;
  73. }
  74.  
  75. /* These two structures from CAR.H are repeated here as comments, because
  76.    the "driver" function receives situation as input and produces con_vec
  77.    as output.
  78. struct situation {       // a car's local situation as seen by the driver
  79.    double cur_rad;       // radius of inner wall of curve (0 means straight)
  80.    double cur_len;       // length of current track segment (angle if curve)
  81.    double to_lft;        // distance to left wall
  82.    double to_rgt;        // distance to right wall
  83.    double to_end;        // how far to end of current track seg. (angle or feet)
  84.    double v;             // the speed of the car, feet per second
  85.    double vn;            // component of v perpendicular to track direction
  86.    double nex_len;       // length of the next track segment (angle if curve)
  87.    double nex_rad;       // radius of inner wall of next segment (or 0)
  88.    double after_rad;     // radius of the segment after that one. (or 0)
  89.    double power_req;     // ratio: power requested by driver to maximum power
  90.    int dead_ahead;       // set when there is a car dead ahead, else 0
  91.    int backward;         // set if cars motion is opposed to track direction
  92.    rel_state* nearby;      // relative states of three cars in front of you
  93.    void* data_ptr;         // pointer to driver's scratchpad RAM area
  94. };
  95.  
  96. struct con_vec { double alpha, vc; };  // control vector, steering & throttle
  97. */
  98.  
  99. // The task of this function is to compute vc and alpha.  A high speed
  100. // car on a track is a little like the keel of a boat; if you set the keel
  101. // at a slight angle to the direction of the oncoming water, you get a large
  102. // force to the side.  That is how we corner the car.  The driver sets the
  103. // car at a slight angle with respect to its direction of motion, this
  104. // cause a force to the side, causing the path of the car to curve.  The
  105. // magnitude of the force is proportional to the angle (alpha) for very
  106. // small alpha, and when there is not much wheel spin.  The wheel spin
  107. // is controlled by vc, which is the rearward speed of the bottom of the
  108. // tire.  When going down the straight at a constant, moderate velocity,
  109. // then vc is equal to the speed of the car.  For acceleration, vc is
  110. // made a little greater than the speed.  For braking, it is made a little
  111. // less.  When accelerating, vc is limited by the power available.
  112. con_vec cntrlR(situation s)
  113. {
  114.    const char name[] = "Rudy";        // This is the robot driver's name!
  115.    static int init_flag = 1;          // cleared by first call
  116.    double speed;                      // target speed for cornering, ft/sec
  117.    double speed_next;                 // target speed for next corner
  118.    con_vec result;                    // This is what is returned.
  119.    double width;                      // track width, feet
  120.    double alpha, vc;           // components of result
  121.    static double alpha_inc = 0.0;  // alpha increment during passing maneuver
  122.    static int counting = 0;    // will be set and counting down when passing
  123.    params* p_ptr;
  124.  
  125.    // This paragraph has nothing to do with car control; it is just
  126.    // to identify the driver by copying its name to a global RAM area:
  127.    // This happens only on the very first call to this function
  128.  
  129.    p_ptr = (params*)s.data_ptr;     // point to the data area
  130.  
  131.    if(init_flag == 1)  {  // first time only, copy name:
  132.       strcpy(glob_name, name);
  133.       init_flag = 0;
  134.       result.alpha = result.vc = 0;
  135.       return result;
  136.    }
  137.  
  138.    if(s.starting)  {  // one time only, set parameter values:
  139.       // These parameters may be adjusted to get better performance:
  140.       p_ptr->CORN_SPD_CON = 7.50; // determines how fast to take corners
  141.       p_ptr->STEER_GAIN = 1.1;     // servo gain, for staying in "lane"
  142.       p_ptr->STEER_DAMP = 0.8;     // servo damping, to prevent "weaving"
  143.       p_ptr->END_ACCEL = .30; // we accelerate until this fraction of length
  144.       p_ptr->END_CORNER = 2.5;// used to decide when to start leaving the corer
  145.       p_ptr->SLIP_LIM = 5.0;  // maximum wheel slip, ft/sec, in wheel_slip()
  146.       p_ptr->SLIP_CON = 500.0; //
  147.       p_ptr->BIAS = .05;        // cornering estimated alpha
  148.       p_ptr->NEAR_END = 1.8;   // widths from corner to start with BIAS
  149.       p_ptr->SHARP_TURN = .07; // change in alpha when attempting to pass
  150.       p_ptr->PASSING_TIME = 90;  // time to stay in passing maneuver, counts
  151.       result.alpha = 0.0;   result.vc = s.v + 40; // accelerate, full power!
  152.       return result;
  153.    }
  154.  
  155.   if(stuck(s.backward, s.v,s.vn, s.to_lft,s.to_rgt, &result.alpha,&result.vc))
  156.       return result;
  157.  
  158.    // Set alpha based on a servo-mechanism approach, trying to stay
  159.    // in the middle of the track, i.e., s.to_left equal to .5 * width:
  160.    width = s.to_lft + s.to_rgt;                        // find width of track
  161.    if(s.cur_rad > 0)  {
  162.       alpha = p_ptr->STEER_GAIN * (s.to_lft - .15 * width) / width;
  163.       alpha += p_ptr->BIAS;
  164.    }
  165.    else if(s.cur_rad < 0)  {
  166.       alpha = p_ptr->STEER_GAIN * (s.to_lft - .85 * width) / width;
  167.       alpha -= p_ptr->BIAS;
  168.    }
  169.    else {      // on straightaway
  170.      alpha = .2 * p_ptr->STEER_GAIN * (s.to_lft - .58 * width) / width;
  171.      if(s.to_end < p_ptr->NEAR_END * width)
  172.        if(s.nex_rad > 0)
  173.           alpha += p_ptr->BIAS;
  174.        else if(s.nex_rad < 0)
  175.           alpha -= p_ptr->BIAS;
  176.    }
  177.    alpha -= p_ptr->STEER_DAMP * s.vn / s.v;  // This is damping, to prevent oscillation
  178.  
  179.    // calculate target speeds for current corner and the next:
  180.    speed = corn_speed(s.cur_rad, p_ptr->CORN_SPD_CON);
  181.    speed_next = corn_speed(s.nex_rad, p_ptr->CORN_SPD_CON);
  182.  
  183.    // now set the tire speed, vc:
  184.    if(s.cur_rad == 0.0)                  // If we are on a straightaway,
  185.       if(s.to_end > p_ptr->END_ACCEL * s.cur_len) // if we are far from the end,
  186.          vc = s.v + p_ptr->SLIP_CON / s.v;          // keep accellerating near full power
  187.       else                    // otherwise,
  188.          vc = wheel_speed(speed_next, s.v, p_ptr->SLIP_LIM);    // brake for next corner
  189.    else                              // If we're in the curve, maintain speed.
  190.       if(s.to_end*ABS(s.cur_rad) > p_ptr->END_CORNER * width*s.v/speed_next)
  191.                   // if we are far from the next corner, stay at "speed".
  192.          vc = wheel_speed(speed, s.v, p_ptr->SLIP_LIM);
  193.       else  {      // but when we near the next corner, adjust to "speed_next"
  194.          vc = wheel_speed(speed_next, s.v, p_ptr->SLIP_LIM);
  195.          alpha += s.cur_rad > 0 ? p_ptr->BIAS : -p_ptr->BIAS;
  196.       }
  197.  
  198.  
  199.    // The passing maneuver:
  200.    if(s.dead_ahead & !counting)  {  // When first encountering the car ahead:
  201.       counting = p_ptr->PASSING_TIME;         // setup the timer,
  202.       if(random(29999) < 15000)        // choose a right or left maneuver:
  203.          alpha_inc = p_ptr->SHARP_TURN;
  204.       else
  205.          alpha_inc = -p_ptr->SHARP_TURN;
  206.    }
  207.    if(counting)  {                // If we are still in the passing maneuver,
  208.       alpha += alpha_inc;              // change alpha
  209.       --counting;                      // count down to zero
  210.    }
  211.  
  212.    result.vc = vc;   result.alpha = alpha;
  213.    return result;
  214. }
  215.